--DO NOT MODIFY THIS FILE. IT IS ALWAYS OVERWRITTEN ON GENERATION.
--Data Schema For Version 0.0.0.0

--CREATE TABLE [actor]
if not exists(select * from sysobjects where name = 'actor' and xtype = 'U')
CREATE TABLE [dbo].[actor] (
[actor_id] [SmallInt] IDENTITY (1, 1) NOT NULL ,
[first_name] [VarChar] (45) NOT NULL ,
[last_name] [VarChar] (45) NOT NULL )

GO

--CREATE TABLE [address]
if not exists(select * from sysobjects where name = 'address' and xtype = 'U')
CREATE TABLE [dbo].[address] (
[address_id] [SmallInt] IDENTITY (1, 1) NOT NULL ,
[address] [VarChar] (50) NOT NULL ,
[address2] [VarChar] (50) NULL ,
[district] [VarChar] (20) NOT NULL ,
[city_id] [SmallInt] NOT NULL ,
[postal_code] [VarChar] (10) NULL ,
[phone] [VarChar] (20) NOT NULL )

GO

--CREATE TABLE [category]
if not exists(select * from sysobjects where name = 'category' and xtype = 'U')
CREATE TABLE [dbo].[category] (
[category_id] [Int] IDENTITY (1, 1) NOT NULL ,
[name] [VarChar] (25) NOT NULL )

GO

--CREATE TABLE [city]
if not exists(select * from sysobjects where name = 'city' and xtype = 'U')
CREATE TABLE [dbo].[city] (
[city_id] [SmallInt] IDENTITY (1, 1) NOT NULL ,
[city] [VarChar] (50) NOT NULL ,
[country_id] [SmallInt] NOT NULL )

GO

--CREATE TABLE [country]
if not exists(select * from sysobjects where name = 'country' and xtype = 'U')
CREATE TABLE [dbo].[country] (
[country_id] [SmallInt] IDENTITY (1, 1) NOT NULL ,
[country] [VarChar] (50) NOT NULL )

GO

--CREATE TABLE [customer]
if not exists(select * from sysobjects where name = 'customer' and xtype = 'U')
CREATE TABLE [dbo].[customer] (
[customer_id] [SmallInt] IDENTITY (1, 1) NOT NULL ,
[store_id] [TinyInt] NOT NULL ,
[first_name] [VarChar] (45) NOT NULL ,
[last_name] [VarChar] (45) NOT NULL ,
[email] [VarChar] (50) NULL ,
[address_id] [SmallInt] NOT NULL ,
[active] [TinyInt] NOT NULL CONSTRAINT [DF__CUSTOMER_ACTIVE] DEFAULT (1))

GO

--CREATE TABLE [film]
if not exists(select * from sysobjects where name = 'film' and xtype = 'U')
CREATE TABLE [dbo].[film] (
[film_id] [SmallInt] IDENTITY (1, 1) NOT NULL ,
[title] [VarChar] (1000) NOT NULL ,
[description] [Text] NULL ,
[release_year] [VarChar] (50) NULL ,
[language_id] [TinyInt] NOT NULL ,
[original_language_id] [TinyInt] NULL ,
[rental_duration] [TinyInt] NOT NULL CONSTRAINT [DF__FILM_RENTAL_DURATION] DEFAULT (3),
[rental_rate] [Decimal] (4, 2) NOT NULL CONSTRAINT [DF__FILM_RENTAL_RATE] DEFAULT (4.99),
[length] [SmallInt] NULL ,
[replacement_cost] [Decimal] (5, 2) NOT NULL CONSTRAINT [DF__FILM_REPLACEMENT_COST] DEFAULT (19.99),
[rating] [VarChar] (50) NULL CONSTRAINT [DF__FILM_RATING] DEFAULT ('G'),
[special_features] [VarChar] (1000) NULL )

GO

--CREATE TABLE [film_actor]
if not exists(select * from sysobjects where name = 'film_actor' and xtype = 'U')
CREATE TABLE [dbo].[film_actor] (
[actor_id] [SmallInt] NOT NULL ,
[film_id] [SmallInt] NOT NULL )

GO

--CREATE TABLE [film_category]
if not exists(select * from sysobjects where name = 'film_category' and xtype = 'U')
CREATE TABLE [dbo].[film_category] (
[film_id] [SmallInt] NOT NULL ,
[category_id] [Int] NOT NULL )

GO

--CREATE TABLE [film_text]
if not exists(select * from sysobjects where name = 'film_text' and xtype = 'U')
CREATE TABLE [dbo].[film_text] (
[film_id] [SmallInt] NOT NULL ,
[title] [VarChar] (255) NOT NULL ,
[description] [Text] NULL )

GO

--CREATE TABLE [inventory]
if not exists(select * from sysobjects where name = 'inventory' and xtype = 'U')
CREATE TABLE [dbo].[inventory] (
[inventory_id] [Int] IDENTITY (1, 1) NOT NULL ,
[film_id] [SmallInt] NOT NULL ,
[store_id] [TinyInt] NOT NULL )

GO

--CREATE TABLE [language]
if not exists(select * from sysobjects where name = 'language' and xtype = 'U')
CREATE TABLE [dbo].[language] (
[language_id] [TinyInt] NOT NULL ,
[name] [Char] (20) NOT NULL )

GO

--CREATE TABLE [payment]
if not exists(select * from sysobjects where name = 'payment' and xtype = 'U')
CREATE TABLE [dbo].[payment] (
[payment_id] [SmallInt] IDENTITY (1, 1) NOT NULL ,
[customer_id] [SmallInt] NOT NULL ,
[staff_id] [TinyInt] NOT NULL ,
[rental_id] [Int] NULL ,
[amount] [Decimal] (5, 2) NOT NULL ,
[payment_date] [DateTime] NOT NULL )

GO

--CREATE TABLE [rental]
if not exists(select * from sysobjects where name = 'rental' and xtype = 'U')
CREATE TABLE [dbo].[rental] (
[rental_id] [Int] IDENTITY (1, 1) NOT NULL ,
[rental_date] [DateTime] NOT NULL ,
[inventory_id] [Int] NOT NULL ,
[customer_id] [SmallInt] NOT NULL ,
[return_date] [DateTime] NULL ,
[staff_id] [TinyInt] NOT NULL )

GO

--CREATE TABLE [staff]
if not exists(select * from sysobjects where name = 'staff' and xtype = 'U')
CREATE TABLE [dbo].[staff] (
[staff_id] [TinyInt] NOT NULL ,
[first_name] [VarChar] (45) NOT NULL ,
[last_name] [VarChar] (45) NOT NULL ,
[address_id] [SmallInt] NOT NULL ,
[picture] [VarChar] (50) NULL ,
[email] [VarChar] (50) NULL ,
[store_id] [TinyInt] NOT NULL ,
[active] [TinyInt] NOT NULL CONSTRAINT [DF__STAFF_ACTIVE] DEFAULT (1),
[username] [VarChar] (16) NOT NULL ,
[password] [VarChar] (40) NULL )

GO

--CREATE TABLE [store]
if not exists(select * from sysobjects where name = 'store' and xtype = 'U')
CREATE TABLE [dbo].[store] (
[store_id] [TinyInt] NOT NULL ,
[manager_staff_id] [TinyInt] NOT NULL ,
[address_id] [SmallInt] NOT NULL )

GO

if exists(select * from sys.objects where name = 'actor' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'actor_id' and o.name = 'actor')
ALTER TABLE [dbo].[actor] ADD [actor_id] [SmallInt] IDENTITY (1, 1) NOT NULL 
if exists(select * from sys.objects where name = 'actor' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'first_name' and o.name = 'actor')
ALTER TABLE [dbo].[actor] ADD [first_name] [VarChar] (45) NOT NULL 
if exists(select * from sys.objects where name = 'actor' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'last_name' and o.name = 'actor')
ALTER TABLE [dbo].[actor] ADD [last_name] [VarChar] (45) NOT NULL 
if exists(select * from sys.objects where name = 'address' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'address_id' and o.name = 'address')
ALTER TABLE [dbo].[address] ADD [address_id] [SmallInt] IDENTITY (1, 1) NOT NULL 
if exists(select * from sys.objects where name = 'address' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'address' and o.name = 'address')
ALTER TABLE [dbo].[address] ADD [address] [VarChar] (50) NOT NULL 
if exists(select * from sys.objects where name = 'address' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'address2' and o.name = 'address')
ALTER TABLE [dbo].[address] ADD [address2] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'address' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'district' and o.name = 'address')
ALTER TABLE [dbo].[address] ADD [district] [VarChar] (20) NOT NULL 
if exists(select * from sys.objects where name = 'address' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'city_id' and o.name = 'address')
ALTER TABLE [dbo].[address] ADD [city_id] [SmallInt] NOT NULL 
if exists(select * from sys.objects where name = 'address' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'postal_code' and o.name = 'address')
ALTER TABLE [dbo].[address] ADD [postal_code] [VarChar] (10) NULL 
if exists(select * from sys.objects where name = 'address' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'phone' and o.name = 'address')
ALTER TABLE [dbo].[address] ADD [phone] [VarChar] (20) NOT NULL 
if exists(select * from sys.objects where name = 'category' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'category_id' and o.name = 'category')
ALTER TABLE [dbo].[category] ADD [category_id] [Int] IDENTITY (1, 1) NOT NULL 
if exists(select * from sys.objects where name = 'category' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'name' and o.name = 'category')
ALTER TABLE [dbo].[category] ADD [name] [VarChar] (25) NOT NULL 
if exists(select * from sys.objects where name = 'city' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'city_id' and o.name = 'city')
ALTER TABLE [dbo].[city] ADD [city_id] [SmallInt] IDENTITY (1, 1) NOT NULL 
if exists(select * from sys.objects where name = 'city' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'city' and o.name = 'city')
ALTER TABLE [dbo].[city] ADD [city] [VarChar] (50) NOT NULL 
if exists(select * from sys.objects where name = 'city' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'country_id' and o.name = 'city')
ALTER TABLE [dbo].[city] ADD [country_id] [SmallInt] NOT NULL 
if exists(select * from sys.objects where name = 'country' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'country_id' and o.name = 'country')
ALTER TABLE [dbo].[country] ADD [country_id] [SmallInt] IDENTITY (1, 1) NOT NULL 
if exists(select * from sys.objects where name = 'country' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'country' and o.name = 'country')
ALTER TABLE [dbo].[country] ADD [country] [VarChar] (50) NOT NULL 
if exists(select * from sys.objects where name = 'customer' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'customer_id' and o.name = 'customer')
ALTER TABLE [dbo].[customer] ADD [customer_id] [SmallInt] IDENTITY (1, 1) NOT NULL 
if exists(select * from sys.objects where name = 'customer' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'store_id' and o.name = 'customer')
ALTER TABLE [dbo].[customer] ADD [store_id] [TinyInt] NOT NULL 
if exists(select * from sys.objects where name = 'customer' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'first_name' and o.name = 'customer')
ALTER TABLE [dbo].[customer] ADD [first_name] [VarChar] (45) NOT NULL 
if exists(select * from sys.objects where name = 'customer' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'last_name' and o.name = 'customer')
ALTER TABLE [dbo].[customer] ADD [last_name] [VarChar] (45) NOT NULL 
if exists(select * from sys.objects where name = 'customer' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'email' and o.name = 'customer')
ALTER TABLE [dbo].[customer] ADD [email] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'customer' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'address_id' and o.name = 'customer')
ALTER TABLE [dbo].[customer] ADD [address_id] [SmallInt] NOT NULL 
if exists(select * from sys.objects where name = 'customer' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'active' and o.name = 'customer')
ALTER TABLE [dbo].[customer] ADD [active] [TinyInt] NOT NULL CONSTRAINT [DF__CUSTOMER_ACTIVE] DEFAULT (1)
if exists(select * from sys.objects where name = 'film' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'film_id' and o.name = 'film')
ALTER TABLE [dbo].[film] ADD [film_id] [SmallInt] IDENTITY (1, 1) NOT NULL 
if exists(select * from sys.objects where name = 'film' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'title' and o.name = 'film')
ALTER TABLE [dbo].[film] ADD [title] [VarChar] (1000) NOT NULL 
if exists(select * from sys.objects where name = 'film' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'description' and o.name = 'film')
ALTER TABLE [dbo].[film] ADD [description] [Text] NULL 
if exists(select * from sys.objects where name = 'film' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'release_year' and o.name = 'film')
ALTER TABLE [dbo].[film] ADD [release_year] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'film' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'language_id' and o.name = 'film')
ALTER TABLE [dbo].[film] ADD [language_id] [TinyInt] NOT NULL 
if exists(select * from sys.objects where name = 'film' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'original_language_id' and o.name = 'film')
ALTER TABLE [dbo].[film] ADD [original_language_id] [TinyInt] NULL 
if exists(select * from sys.objects where name = 'film' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'rental_duration' and o.name = 'film')
ALTER TABLE [dbo].[film] ADD [rental_duration] [TinyInt] NOT NULL CONSTRAINT [DF__FILM_RENTAL_DURATION] DEFAULT (3)
if exists(select * from sys.objects where name = 'film' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'rental_rate' and o.name = 'film')
ALTER TABLE [dbo].[film] ADD [rental_rate] [Decimal] (4, 2) NOT NULL CONSTRAINT [DF__FILM_RENTAL_RATE] DEFAULT (4.99)
if exists(select * from sys.objects where name = 'film' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'length' and o.name = 'film')
ALTER TABLE [dbo].[film] ADD [length] [SmallInt] NULL 
if exists(select * from sys.objects where name = 'film' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'replacement_cost' and o.name = 'film')
ALTER TABLE [dbo].[film] ADD [replacement_cost] [Decimal] (5, 2) NOT NULL CONSTRAINT [DF__FILM_REPLACEMENT_COST] DEFAULT (19.99)
if exists(select * from sys.objects where name = 'film' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'rating' and o.name = 'film')
ALTER TABLE [dbo].[film] ADD [rating] [VarChar] (50) NULL CONSTRAINT [DF__FILM_RATING] DEFAULT ('G')
if exists(select * from sys.objects where name = 'film' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'special_features' and o.name = 'film')
ALTER TABLE [dbo].[film] ADD [special_features] [VarChar] (1000) NULL 
if exists(select * from sys.objects where name = 'film_actor' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'actor_id' and o.name = 'film_actor')
ALTER TABLE [dbo].[film_actor] ADD [actor_id] [SmallInt] NOT NULL 
if exists(select * from sys.objects where name = 'film_actor' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'film_id' and o.name = 'film_actor')
ALTER TABLE [dbo].[film_actor] ADD [film_id] [SmallInt] NOT NULL 
if exists(select * from sys.objects where name = 'film_category' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'film_id' and o.name = 'film_category')
ALTER TABLE [dbo].[film_category] ADD [film_id] [SmallInt] NOT NULL 
if exists(select * from sys.objects where name = 'film_category' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'category_id' and o.name = 'film_category')
ALTER TABLE [dbo].[film_category] ADD [category_id] [Int] NOT NULL 
if exists(select * from sys.objects where name = 'film_text' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'film_id' and o.name = 'film_text')
ALTER TABLE [dbo].[film_text] ADD [film_id] [SmallInt] NOT NULL 
if exists(select * from sys.objects where name = 'film_text' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'title' and o.name = 'film_text')
ALTER TABLE [dbo].[film_text] ADD [title] [VarChar] (255) NOT NULL 
if exists(select * from sys.objects where name = 'film_text' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'description' and o.name = 'film_text')
ALTER TABLE [dbo].[film_text] ADD [description] [Text] NULL 
if exists(select * from sys.objects where name = 'inventory' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'inventory_id' and o.name = 'inventory')
ALTER TABLE [dbo].[inventory] ADD [inventory_id] [Int] IDENTITY (1, 1) NOT NULL 
if exists(select * from sys.objects where name = 'inventory' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'film_id' and o.name = 'inventory')
ALTER TABLE [dbo].[inventory] ADD [film_id] [SmallInt] NOT NULL 
if exists(select * from sys.objects where name = 'inventory' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'store_id' and o.name = 'inventory')
ALTER TABLE [dbo].[inventory] ADD [store_id] [TinyInt] NOT NULL 
if exists(select * from sys.objects where name = 'language' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'language_id' and o.name = 'language')
ALTER TABLE [dbo].[language] ADD [language_id] [TinyInt] NOT NULL 
if exists(select * from sys.objects where name = 'language' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'name' and o.name = 'language')
ALTER TABLE [dbo].[language] ADD [name] [Char] (20) NOT NULL 
if exists(select * from sys.objects where name = 'payment' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'payment_id' and o.name = 'payment')
ALTER TABLE [dbo].[payment] ADD [payment_id] [SmallInt] IDENTITY (1, 1) NOT NULL 
if exists(select * from sys.objects where name = 'payment' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'customer_id' and o.name = 'payment')
ALTER TABLE [dbo].[payment] ADD [customer_id] [SmallInt] NOT NULL 
if exists(select * from sys.objects where name = 'payment' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'staff_id' and o.name = 'payment')
ALTER TABLE [dbo].[payment] ADD [staff_id] [TinyInt] NOT NULL 
if exists(select * from sys.objects where name = 'payment' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'rental_id' and o.name = 'payment')
ALTER TABLE [dbo].[payment] ADD [rental_id] [Int] NULL 
if exists(select * from sys.objects where name = 'payment' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'amount' and o.name = 'payment')
ALTER TABLE [dbo].[payment] ADD [amount] [Decimal] (5, 2) NOT NULL 
if exists(select * from sys.objects where name = 'payment' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'payment_date' and o.name = 'payment')
ALTER TABLE [dbo].[payment] ADD [payment_date] [DateTime] NOT NULL 
if exists(select * from sys.objects where name = 'rental' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'rental_id' and o.name = 'rental')
ALTER TABLE [dbo].[rental] ADD [rental_id] [Int] IDENTITY (1, 1) NOT NULL 
if exists(select * from sys.objects where name = 'rental' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'rental_date' and o.name = 'rental')
ALTER TABLE [dbo].[rental] ADD [rental_date] [DateTime] NOT NULL 
if exists(select * from sys.objects where name = 'rental' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'inventory_id' and o.name = 'rental')
ALTER TABLE [dbo].[rental] ADD [inventory_id] [Int] NOT NULL 
if exists(select * from sys.objects where name = 'rental' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'customer_id' and o.name = 'rental')
ALTER TABLE [dbo].[rental] ADD [customer_id] [SmallInt] NOT NULL 
if exists(select * from sys.objects where name = 'rental' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'return_date' and o.name = 'rental')
ALTER TABLE [dbo].[rental] ADD [return_date] [DateTime] NULL 
if exists(select * from sys.objects where name = 'rental' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'staff_id' and o.name = 'rental')
ALTER TABLE [dbo].[rental] ADD [staff_id] [TinyInt] NOT NULL 
if exists(select * from sys.objects where name = 'staff' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'staff_id' and o.name = 'staff')
ALTER TABLE [dbo].[staff] ADD [staff_id] [TinyInt] NOT NULL 
if exists(select * from sys.objects where name = 'staff' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'first_name' and o.name = 'staff')
ALTER TABLE [dbo].[staff] ADD [first_name] [VarChar] (45) NOT NULL 
if exists(select * from sys.objects where name = 'staff' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'last_name' and o.name = 'staff')
ALTER TABLE [dbo].[staff] ADD [last_name] [VarChar] (45) NOT NULL 
if exists(select * from sys.objects where name = 'staff' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'address_id' and o.name = 'staff')
ALTER TABLE [dbo].[staff] ADD [address_id] [SmallInt] NOT NULL 
if exists(select * from sys.objects where name = 'staff' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'picture' and o.name = 'staff')
ALTER TABLE [dbo].[staff] ADD [picture] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'staff' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'email' and o.name = 'staff')
ALTER TABLE [dbo].[staff] ADD [email] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'staff' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'store_id' and o.name = 'staff')
ALTER TABLE [dbo].[staff] ADD [store_id] [TinyInt] NOT NULL 
if exists(select * from sys.objects where name = 'staff' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'active' and o.name = 'staff')
ALTER TABLE [dbo].[staff] ADD [active] [TinyInt] NOT NULL CONSTRAINT [DF__STAFF_ACTIVE] DEFAULT (1)
if exists(select * from sys.objects where name = 'staff' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'username' and o.name = 'staff')
ALTER TABLE [dbo].[staff] ADD [username] [VarChar] (16) NOT NULL 
if exists(select * from sys.objects where name = 'staff' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'password' and o.name = 'staff')
ALTER TABLE [dbo].[staff] ADD [password] [VarChar] (40) NULL 
if exists(select * from sys.objects where name = 'store' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'store_id' and o.name = 'store')
ALTER TABLE [dbo].[store] ADD [store_id] [TinyInt] NOT NULL 
if exists(select * from sys.objects where name = 'store' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'manager_staff_id' and o.name = 'store')
ALTER TABLE [dbo].[store] ADD [manager_staff_id] [TinyInt] NOT NULL 
if exists(select * from sys.objects where name = 'store' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'address_id' and o.name = 'store')
ALTER TABLE [dbo].[store] ADD [address_id] [SmallInt] NOT NULL 
GO

--##SECTION BEGIN [AUDIT TRAIL]

--REMOVE AUDIT TRAIL CREATE FOR TABLE [actor]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'actor')
ALTER TABLE [dbo].[actor] DROP COLUMN [CreatedBy]
if exists (select * from sys.objects where name = 'DF__ACTOR_CREATEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[actor] DROP CONSTRAINT [DF__ACTOR_CREATEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'actor')
ALTER TABLE [dbo].[actor] DROP COLUMN [CreatedDate]

--REMOVE AUDIT TRAIL MODIFY FOR TABLE [actor]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'actor')
ALTER TABLE [dbo].[actor] DROP COLUMN [ModifiedBy]
if exists (select * from sys.objects where name = 'DF__ACTOR_MODIFIEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[actor] DROP CONSTRAINT [DF__ACTOR_MODIFIEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'actor')
ALTER TABLE [dbo].[actor] DROP COLUMN [ModifiedDate]

--REMOVE AUDIT TRAIL TIMESTAMP FOR TABLE [actor]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'actor')
ALTER TABLE [dbo].[actor] DROP COLUMN [Timestamp]

GO

--REMOVE AUDIT TRAIL CREATE FOR TABLE [address]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'address')
ALTER TABLE [dbo].[address] DROP COLUMN [CreatedBy]
if exists (select * from sys.objects where name = 'DF__ADDRESS_CREATEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[address] DROP CONSTRAINT [DF__ADDRESS_CREATEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'address')
ALTER TABLE [dbo].[address] DROP COLUMN [CreatedDate]

--REMOVE AUDIT TRAIL MODIFY FOR TABLE [address]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'address')
ALTER TABLE [dbo].[address] DROP COLUMN [ModifiedBy]
if exists (select * from sys.objects where name = 'DF__ADDRESS_MODIFIEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[address] DROP CONSTRAINT [DF__ADDRESS_MODIFIEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'address')
ALTER TABLE [dbo].[address] DROP COLUMN [ModifiedDate]

--REMOVE AUDIT TRAIL TIMESTAMP FOR TABLE [address]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'address')
ALTER TABLE [dbo].[address] DROP COLUMN [Timestamp]

GO

--REMOVE AUDIT TRAIL CREATE FOR TABLE [category]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'category')
ALTER TABLE [dbo].[category] DROP COLUMN [CreatedBy]
if exists (select * from sys.objects where name = 'DF__CATEGORY_CREATEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[category] DROP CONSTRAINT [DF__CATEGORY_CREATEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'category')
ALTER TABLE [dbo].[category] DROP COLUMN [CreatedDate]

--REMOVE AUDIT TRAIL MODIFY FOR TABLE [category]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'category')
ALTER TABLE [dbo].[category] DROP COLUMN [ModifiedBy]
if exists (select * from sys.objects where name = 'DF__CATEGORY_MODIFIEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[category] DROP CONSTRAINT [DF__CATEGORY_MODIFIEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'category')
ALTER TABLE [dbo].[category] DROP COLUMN [ModifiedDate]

--REMOVE AUDIT TRAIL TIMESTAMP FOR TABLE [category]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'category')
ALTER TABLE [dbo].[category] DROP COLUMN [Timestamp]

GO

--REMOVE AUDIT TRAIL CREATE FOR TABLE [city]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'city')
ALTER TABLE [dbo].[city] DROP COLUMN [CreatedBy]
if exists (select * from sys.objects where name = 'DF__CITY_CREATEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[city] DROP CONSTRAINT [DF__CITY_CREATEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'city')
ALTER TABLE [dbo].[city] DROP COLUMN [CreatedDate]

--REMOVE AUDIT TRAIL MODIFY FOR TABLE [city]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'city')
ALTER TABLE [dbo].[city] DROP COLUMN [ModifiedBy]
if exists (select * from sys.objects where name = 'DF__CITY_MODIFIEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[city] DROP CONSTRAINT [DF__CITY_MODIFIEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'city')
ALTER TABLE [dbo].[city] DROP COLUMN [ModifiedDate]

--REMOVE AUDIT TRAIL TIMESTAMP FOR TABLE [city]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'city')
ALTER TABLE [dbo].[city] DROP COLUMN [Timestamp]

GO

--REMOVE AUDIT TRAIL CREATE FOR TABLE [country]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'country')
ALTER TABLE [dbo].[country] DROP COLUMN [CreatedBy]
if exists (select * from sys.objects where name = 'DF__COUNTRY_CREATEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[country] DROP CONSTRAINT [DF__COUNTRY_CREATEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'country')
ALTER TABLE [dbo].[country] DROP COLUMN [CreatedDate]

--REMOVE AUDIT TRAIL MODIFY FOR TABLE [country]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'country')
ALTER TABLE [dbo].[country] DROP COLUMN [ModifiedBy]
if exists (select * from sys.objects where name = 'DF__COUNTRY_MODIFIEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[country] DROP CONSTRAINT [DF__COUNTRY_MODIFIEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'country')
ALTER TABLE [dbo].[country] DROP COLUMN [ModifiedDate]

--REMOVE AUDIT TRAIL TIMESTAMP FOR TABLE [country]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'country')
ALTER TABLE [dbo].[country] DROP COLUMN [Timestamp]

GO

--REMOVE AUDIT TRAIL CREATE FOR TABLE [customer]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'customer')
ALTER TABLE [dbo].[customer] DROP COLUMN [CreatedBy]
if exists (select * from sys.objects where name = 'DF__CUSTOMER_CREATEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[customer] DROP CONSTRAINT [DF__CUSTOMER_CREATEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'customer')
ALTER TABLE [dbo].[customer] DROP COLUMN [CreatedDate]

--REMOVE AUDIT TRAIL MODIFY FOR TABLE [customer]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'customer')
ALTER TABLE [dbo].[customer] DROP COLUMN [ModifiedBy]
if exists (select * from sys.objects where name = 'DF__CUSTOMER_MODIFIEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[customer] DROP CONSTRAINT [DF__CUSTOMER_MODIFIEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'customer')
ALTER TABLE [dbo].[customer] DROP COLUMN [ModifiedDate]

--REMOVE AUDIT TRAIL TIMESTAMP FOR TABLE [customer]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'customer')
ALTER TABLE [dbo].[customer] DROP COLUMN [Timestamp]

GO

--REMOVE AUDIT TRAIL CREATE FOR TABLE [film]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'film')
ALTER TABLE [dbo].[film] DROP COLUMN [CreatedBy]
if exists (select * from sys.objects where name = 'DF__FILM_CREATEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[film] DROP CONSTRAINT [DF__FILM_CREATEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'film')
ALTER TABLE [dbo].[film] DROP COLUMN [CreatedDate]

--REMOVE AUDIT TRAIL MODIFY FOR TABLE [film]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'film')
ALTER TABLE [dbo].[film] DROP COLUMN [ModifiedBy]
if exists (select * from sys.objects where name = 'DF__FILM_MODIFIEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[film] DROP CONSTRAINT [DF__FILM_MODIFIEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'film')
ALTER TABLE [dbo].[film] DROP COLUMN [ModifiedDate]

--REMOVE AUDIT TRAIL TIMESTAMP FOR TABLE [film]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'film')
ALTER TABLE [dbo].[film] DROP COLUMN [Timestamp]

GO

--REMOVE AUDIT TRAIL CREATE FOR TABLE [film_actor]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'film_actor')
ALTER TABLE [dbo].[film_actor] DROP COLUMN [CreatedBy]
if exists (select * from sys.objects where name = 'DF__FILM_ACTOR_CREATEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[film_actor] DROP CONSTRAINT [DF__FILM_ACTOR_CREATEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'film_actor')
ALTER TABLE [dbo].[film_actor] DROP COLUMN [CreatedDate]

--REMOVE AUDIT TRAIL MODIFY FOR TABLE [film_actor]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'film_actor')
ALTER TABLE [dbo].[film_actor] DROP COLUMN [ModifiedBy]
if exists (select * from sys.objects where name = 'DF__FILM_ACTOR_MODIFIEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[film_actor] DROP CONSTRAINT [DF__FILM_ACTOR_MODIFIEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'film_actor')
ALTER TABLE [dbo].[film_actor] DROP COLUMN [ModifiedDate]

--REMOVE AUDIT TRAIL TIMESTAMP FOR TABLE [film_actor]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'film_actor')
ALTER TABLE [dbo].[film_actor] DROP COLUMN [Timestamp]

GO

--REMOVE AUDIT TRAIL CREATE FOR TABLE [film_category]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'film_category')
ALTER TABLE [dbo].[film_category] DROP COLUMN [CreatedBy]
if exists (select * from sys.objects where name = 'DF__FILM_CATEGORY_CREATEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[film_category] DROP CONSTRAINT [DF__FILM_CATEGORY_CREATEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'film_category')
ALTER TABLE [dbo].[film_category] DROP COLUMN [CreatedDate]

--REMOVE AUDIT TRAIL MODIFY FOR TABLE [film_category]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'film_category')
ALTER TABLE [dbo].[film_category] DROP COLUMN [ModifiedBy]
if exists (select * from sys.objects where name = 'DF__FILM_CATEGORY_MODIFIEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[film_category] DROP CONSTRAINT [DF__FILM_CATEGORY_MODIFIEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'film_category')
ALTER TABLE [dbo].[film_category] DROP COLUMN [ModifiedDate]

--REMOVE AUDIT TRAIL TIMESTAMP FOR TABLE [film_category]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'film_category')
ALTER TABLE [dbo].[film_category] DROP COLUMN [Timestamp]

GO

--REMOVE AUDIT TRAIL CREATE FOR TABLE [film_text]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'film_text')
ALTER TABLE [dbo].[film_text] DROP COLUMN [CreatedBy]
if exists (select * from sys.objects where name = 'DF__FILM_TEXT_CREATEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[film_text] DROP CONSTRAINT [DF__FILM_TEXT_CREATEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'film_text')
ALTER TABLE [dbo].[film_text] DROP COLUMN [CreatedDate]

--REMOVE AUDIT TRAIL MODIFY FOR TABLE [film_text]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'film_text')
ALTER TABLE [dbo].[film_text] DROP COLUMN [ModifiedBy]
if exists (select * from sys.objects where name = 'DF__FILM_TEXT_MODIFIEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[film_text] DROP CONSTRAINT [DF__FILM_TEXT_MODIFIEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'film_text')
ALTER TABLE [dbo].[film_text] DROP COLUMN [ModifiedDate]

--REMOVE AUDIT TRAIL TIMESTAMP FOR TABLE [film_text]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'film_text')
ALTER TABLE [dbo].[film_text] DROP COLUMN [Timestamp]

GO

--REMOVE AUDIT TRAIL CREATE FOR TABLE [inventory]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'inventory')
ALTER TABLE [dbo].[inventory] DROP COLUMN [CreatedBy]
if exists (select * from sys.objects where name = 'DF__INVENTORY_CREATEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[inventory] DROP CONSTRAINT [DF__INVENTORY_CREATEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'inventory')
ALTER TABLE [dbo].[inventory] DROP COLUMN [CreatedDate]

--REMOVE AUDIT TRAIL MODIFY FOR TABLE [inventory]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'inventory')
ALTER TABLE [dbo].[inventory] DROP COLUMN [ModifiedBy]
if exists (select * from sys.objects where name = 'DF__INVENTORY_MODIFIEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[inventory] DROP CONSTRAINT [DF__INVENTORY_MODIFIEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'inventory')
ALTER TABLE [dbo].[inventory] DROP COLUMN [ModifiedDate]

--REMOVE AUDIT TRAIL TIMESTAMP FOR TABLE [inventory]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'inventory')
ALTER TABLE [dbo].[inventory] DROP COLUMN [Timestamp]

GO

--REMOVE AUDIT TRAIL CREATE FOR TABLE [language]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'language')
ALTER TABLE [dbo].[language] DROP COLUMN [CreatedBy]
if exists (select * from sys.objects where name = 'DF__LANGUAGE_CREATEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[language] DROP CONSTRAINT [DF__LANGUAGE_CREATEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'language')
ALTER TABLE [dbo].[language] DROP COLUMN [CreatedDate]

--REMOVE AUDIT TRAIL MODIFY FOR TABLE [language]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'language')
ALTER TABLE [dbo].[language] DROP COLUMN [ModifiedBy]
if exists (select * from sys.objects where name = 'DF__LANGUAGE_MODIFIEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[language] DROP CONSTRAINT [DF__LANGUAGE_MODIFIEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'language')
ALTER TABLE [dbo].[language] DROP COLUMN [ModifiedDate]

--REMOVE AUDIT TRAIL TIMESTAMP FOR TABLE [language]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'language')
ALTER TABLE [dbo].[language] DROP COLUMN [Timestamp]

GO

--REMOVE AUDIT TRAIL CREATE FOR TABLE [payment]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'payment')
ALTER TABLE [dbo].[payment] DROP COLUMN [CreatedBy]
if exists (select * from sys.objects where name = 'DF__PAYMENT_CREATEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[payment] DROP CONSTRAINT [DF__PAYMENT_CREATEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'payment')
ALTER TABLE [dbo].[payment] DROP COLUMN [CreatedDate]

--REMOVE AUDIT TRAIL MODIFY FOR TABLE [payment]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'payment')
ALTER TABLE [dbo].[payment] DROP COLUMN [ModifiedBy]
if exists (select * from sys.objects where name = 'DF__PAYMENT_MODIFIEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[payment] DROP CONSTRAINT [DF__PAYMENT_MODIFIEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'payment')
ALTER TABLE [dbo].[payment] DROP COLUMN [ModifiedDate]

--REMOVE AUDIT TRAIL TIMESTAMP FOR TABLE [payment]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'payment')
ALTER TABLE [dbo].[payment] DROP COLUMN [Timestamp]

GO

--REMOVE AUDIT TRAIL CREATE FOR TABLE [rental]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'rental')
ALTER TABLE [dbo].[rental] DROP COLUMN [CreatedBy]
if exists (select * from sys.objects where name = 'DF__RENTAL_CREATEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[rental] DROP CONSTRAINT [DF__RENTAL_CREATEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'rental')
ALTER TABLE [dbo].[rental] DROP COLUMN [CreatedDate]

--REMOVE AUDIT TRAIL MODIFY FOR TABLE [rental]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'rental')
ALTER TABLE [dbo].[rental] DROP COLUMN [ModifiedBy]
if exists (select * from sys.objects where name = 'DF__RENTAL_MODIFIEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[rental] DROP CONSTRAINT [DF__RENTAL_MODIFIEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'rental')
ALTER TABLE [dbo].[rental] DROP COLUMN [ModifiedDate]

--REMOVE AUDIT TRAIL TIMESTAMP FOR TABLE [rental]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'rental')
ALTER TABLE [dbo].[rental] DROP COLUMN [Timestamp]

GO

--REMOVE AUDIT TRAIL CREATE FOR TABLE [staff]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'staff')
ALTER TABLE [dbo].[staff] DROP COLUMN [CreatedBy]
if exists (select * from sys.objects where name = 'DF__STAFF_CREATEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[staff] DROP CONSTRAINT [DF__STAFF_CREATEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'staff')
ALTER TABLE [dbo].[staff] DROP COLUMN [CreatedDate]

--REMOVE AUDIT TRAIL MODIFY FOR TABLE [staff]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'staff')
ALTER TABLE [dbo].[staff] DROP COLUMN [ModifiedBy]
if exists (select * from sys.objects where name = 'DF__STAFF_MODIFIEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[staff] DROP CONSTRAINT [DF__STAFF_MODIFIEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'staff')
ALTER TABLE [dbo].[staff] DROP COLUMN [ModifiedDate]

--REMOVE AUDIT TRAIL TIMESTAMP FOR TABLE [staff]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'staff')
ALTER TABLE [dbo].[staff] DROP COLUMN [Timestamp]

GO

--REMOVE AUDIT TRAIL CREATE FOR TABLE [store]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'store')
ALTER TABLE [dbo].[store] DROP COLUMN [CreatedBy]
if exists (select * from sys.objects where name = 'DF__STORE_CREATEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[store] DROP CONSTRAINT [DF__STORE_CREATEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'store')
ALTER TABLE [dbo].[store] DROP COLUMN [CreatedDate]

--REMOVE AUDIT TRAIL MODIFY FOR TABLE [store]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'store')
ALTER TABLE [dbo].[store] DROP COLUMN [ModifiedBy]
if exists (select * from sys.objects where name = 'DF__STORE_MODIFIEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[store] DROP CONSTRAINT [DF__STORE_MODIFIEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'store')
ALTER TABLE [dbo].[store] DROP COLUMN [ModifiedDate]

--REMOVE AUDIT TRAIL TIMESTAMP FOR TABLE [store]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'store')
ALTER TABLE [dbo].[store] DROP COLUMN [Timestamp]

GO

--##SECTION END [AUDIT TRAIL]

--##SECTION BEGIN [RENAME PK]

--RENAME EXISTING PRIMARY KEYS IF NECESSARY
DECLARE @pkfixactor varchar(500)
SET @pkfixactor = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'actor')
if @pkfixactor <> '' and (BINARY_CHECKSUM(@pkfixactor) <> BINARY_CHECKSUM('PK_ACTOR')) exec('sp_rename '''+@pkfixactor+''', ''PK_ACTOR''')
DECLARE @pkfixaddress varchar(500)
SET @pkfixaddress = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'address')
if @pkfixaddress <> '' and (BINARY_CHECKSUM(@pkfixaddress) <> BINARY_CHECKSUM('PK_ADDRESS')) exec('sp_rename '''+@pkfixaddress+''', ''PK_ADDRESS''')
DECLARE @pkfixcategory varchar(500)
SET @pkfixcategory = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'category')
if @pkfixcategory <> '' and (BINARY_CHECKSUM(@pkfixcategory) <> BINARY_CHECKSUM('PK_CATEGORY')) exec('sp_rename '''+@pkfixcategory+''', ''PK_CATEGORY''')
DECLARE @pkfixcity varchar(500)
SET @pkfixcity = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'city')
if @pkfixcity <> '' and (BINARY_CHECKSUM(@pkfixcity) <> BINARY_CHECKSUM('PK_CITY')) exec('sp_rename '''+@pkfixcity+''', ''PK_CITY''')
DECLARE @pkfixcountry varchar(500)
SET @pkfixcountry = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'country')
if @pkfixcountry <> '' and (BINARY_CHECKSUM(@pkfixcountry) <> BINARY_CHECKSUM('PK_COUNTRY')) exec('sp_rename '''+@pkfixcountry+''', ''PK_COUNTRY''')
DECLARE @pkfixcustomer varchar(500)
SET @pkfixcustomer = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'customer')
if @pkfixcustomer <> '' and (BINARY_CHECKSUM(@pkfixcustomer) <> BINARY_CHECKSUM('PK_CUSTOMER')) exec('sp_rename '''+@pkfixcustomer+''', ''PK_CUSTOMER''')
DECLARE @pkfixfilm varchar(500)
SET @pkfixfilm = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'film')
if @pkfixfilm <> '' and (BINARY_CHECKSUM(@pkfixfilm) <> BINARY_CHECKSUM('PK_FILM')) exec('sp_rename '''+@pkfixfilm+''', ''PK_FILM''')
DECLARE @pkfixfilm_actor varchar(500)
SET @pkfixfilm_actor = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'film_actor')
if @pkfixfilm_actor <> '' and (BINARY_CHECKSUM(@pkfixfilm_actor) <> BINARY_CHECKSUM('PK_FILM_ACTOR')) exec('sp_rename '''+@pkfixfilm_actor+''', ''PK_FILM_ACTOR''')
DECLARE @pkfixfilm_category varchar(500)
SET @pkfixfilm_category = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'film_category')
if @pkfixfilm_category <> '' and (BINARY_CHECKSUM(@pkfixfilm_category) <> BINARY_CHECKSUM('PK_FILM_CATEGORY')) exec('sp_rename '''+@pkfixfilm_category+''', ''PK_FILM_CATEGORY''')
DECLARE @pkfixfilm_text varchar(500)
SET @pkfixfilm_text = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'film_text')
if @pkfixfilm_text <> '' and (BINARY_CHECKSUM(@pkfixfilm_text) <> BINARY_CHECKSUM('PK_FILM_TEXT')) exec('sp_rename '''+@pkfixfilm_text+''', ''PK_FILM_TEXT''')
DECLARE @pkfixinventory varchar(500)
SET @pkfixinventory = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'inventory')
if @pkfixinventory <> '' and (BINARY_CHECKSUM(@pkfixinventory) <> BINARY_CHECKSUM('PK_INVENTORY')) exec('sp_rename '''+@pkfixinventory+''', ''PK_INVENTORY''')
DECLARE @pkfixlanguage varchar(500)
SET @pkfixlanguage = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'language')
if @pkfixlanguage <> '' and (BINARY_CHECKSUM(@pkfixlanguage) <> BINARY_CHECKSUM('PK_LANGUAGE')) exec('sp_rename '''+@pkfixlanguage+''', ''PK_LANGUAGE''')
DECLARE @pkfixpayment varchar(500)
SET @pkfixpayment = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'payment')
if @pkfixpayment <> '' and (BINARY_CHECKSUM(@pkfixpayment) <> BINARY_CHECKSUM('PK_PAYMENT')) exec('sp_rename '''+@pkfixpayment+''', ''PK_PAYMENT''')
DECLARE @pkfixrental varchar(500)
SET @pkfixrental = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'rental')
if @pkfixrental <> '' and (BINARY_CHECKSUM(@pkfixrental) <> BINARY_CHECKSUM('PK_RENTAL')) exec('sp_rename '''+@pkfixrental+''', ''PK_RENTAL''')
DECLARE @pkfixstaff varchar(500)
SET @pkfixstaff = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'staff')
if @pkfixstaff <> '' and (BINARY_CHECKSUM(@pkfixstaff) <> BINARY_CHECKSUM('PK_STAFF')) exec('sp_rename '''+@pkfixstaff+''', ''PK_STAFF''')
DECLARE @pkfixstore varchar(500)
SET @pkfixstore = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'store')
if @pkfixstore <> '' and (BINARY_CHECKSUM(@pkfixstore) <> BINARY_CHECKSUM('PK_STORE')) exec('sp_rename '''+@pkfixstore+''', ''PK_STORE''')
GO

--##SECTION END [RENAME PK]

--##SECTION BEGIN [DROP PK]

--##SECTION END [DROP PK]

--##SECTION BEGIN [CREATE PK]

--PRIMARY KEY FOR TABLE [actor]
if not exists(select * from sysobjects where name = 'PK_ACTOR' and xtype = 'PK')
ALTER TABLE [dbo].[actor] WITH NOCHECK ADD 
CONSTRAINT [PK_ACTOR] PRIMARY KEY NONCLUSTERED
(
	[actor_id]
)
GO

--PRIMARY KEY FOR TABLE [address]
if not exists(select * from sysobjects where name = 'PK_ADDRESS' and xtype = 'PK')
ALTER TABLE [dbo].[address] WITH NOCHECK ADD 
CONSTRAINT [PK_ADDRESS] PRIMARY KEY NONCLUSTERED
(
	[address_id]
)
GO

--PRIMARY KEY FOR TABLE [category]
if not exists(select * from sysobjects where name = 'PK_CATEGORY' and xtype = 'PK')
ALTER TABLE [dbo].[category] WITH NOCHECK ADD 
CONSTRAINT [PK_CATEGORY] PRIMARY KEY NONCLUSTERED
(
	[category_id]
)
GO

--PRIMARY KEY FOR TABLE [city]
if not exists(select * from sysobjects where name = 'PK_CITY' and xtype = 'PK')
ALTER TABLE [dbo].[city] WITH NOCHECK ADD 
CONSTRAINT [PK_CITY] PRIMARY KEY NONCLUSTERED
(
	[city_id]
)
GO

--PRIMARY KEY FOR TABLE [country]
if not exists(select * from sysobjects where name = 'PK_COUNTRY' and xtype = 'PK')
ALTER TABLE [dbo].[country] WITH NOCHECK ADD 
CONSTRAINT [PK_COUNTRY] PRIMARY KEY NONCLUSTERED
(
	[country_id]
)
GO

--PRIMARY KEY FOR TABLE [customer]
if not exists(select * from sysobjects where name = 'PK_CUSTOMER' and xtype = 'PK')
ALTER TABLE [dbo].[customer] WITH NOCHECK ADD 
CONSTRAINT [PK_CUSTOMER] PRIMARY KEY NONCLUSTERED
(
	[customer_id]
)
GO

--PRIMARY KEY FOR TABLE [film]
if not exists(select * from sysobjects where name = 'PK_FILM' and xtype = 'PK')
ALTER TABLE [dbo].[film] WITH NOCHECK ADD 
CONSTRAINT [PK_FILM] PRIMARY KEY NONCLUSTERED
(
	[film_id]
)
GO

--PRIMARY KEY FOR TABLE [film_actor]
if not exists(select * from sysobjects where name = 'PK_FILM_ACTOR' and xtype = 'PK')
ALTER TABLE [dbo].[film_actor] WITH NOCHECK ADD 
CONSTRAINT [PK_FILM_ACTOR] PRIMARY KEY NONCLUSTERED
(
	[actor_id],
	[film_id]
)
GO

--PRIMARY KEY FOR TABLE [film_category]
if not exists(select * from sysobjects where name = 'PK_FILM_CATEGORY' and xtype = 'PK')
ALTER TABLE [dbo].[film_category] WITH NOCHECK ADD 
CONSTRAINT [PK_FILM_CATEGORY] PRIMARY KEY NONCLUSTERED
(
	[film_id],
	[category_id]
)
GO

--PRIMARY KEY FOR TABLE [film_text]
if not exists(select * from sysobjects where name = 'PK_FILM_TEXT' and xtype = 'PK')
ALTER TABLE [dbo].[film_text] WITH NOCHECK ADD 
CONSTRAINT [PK_FILM_TEXT] PRIMARY KEY NONCLUSTERED
(
	[film_id]
)
GO

--PRIMARY KEY FOR TABLE [inventory]
if not exists(select * from sysobjects where name = 'PK_INVENTORY' and xtype = 'PK')
ALTER TABLE [dbo].[inventory] WITH NOCHECK ADD 
CONSTRAINT [PK_INVENTORY] PRIMARY KEY NONCLUSTERED
(
	[inventory_id]
)
GO

--PRIMARY KEY FOR TABLE [language]
if not exists(select * from sysobjects where name = 'PK_LANGUAGE' and xtype = 'PK')
ALTER TABLE [dbo].[language] WITH NOCHECK ADD 
CONSTRAINT [PK_LANGUAGE] PRIMARY KEY NONCLUSTERED
(
	[language_id]
)
GO

--PRIMARY KEY FOR TABLE [payment]
if not exists(select * from sysobjects where name = 'PK_PAYMENT' and xtype = 'PK')
ALTER TABLE [dbo].[payment] WITH NOCHECK ADD 
CONSTRAINT [PK_PAYMENT] PRIMARY KEY NONCLUSTERED
(
	[payment_id]
)
GO

--PRIMARY KEY FOR TABLE [rental]
if not exists(select * from sysobjects where name = 'PK_RENTAL' and xtype = 'PK')
ALTER TABLE [dbo].[rental] WITH NOCHECK ADD 
CONSTRAINT [PK_RENTAL] PRIMARY KEY NONCLUSTERED
(
	[rental_id]
)
GO

--PRIMARY KEY FOR TABLE [staff]
if not exists(select * from sysobjects where name = 'PK_STAFF' and xtype = 'PK')
ALTER TABLE [dbo].[staff] WITH NOCHECK ADD 
CONSTRAINT [PK_STAFF] PRIMARY KEY NONCLUSTERED
(
	[staff_id]
)
GO

--PRIMARY KEY FOR TABLE [store]
if not exists(select * from sysobjects where name = 'PK_STORE' and xtype = 'PK')
ALTER TABLE [dbo].[store] WITH NOCHECK ADD 
CONSTRAINT [PK_STORE] PRIMARY KEY NONCLUSTERED
(
	[store_id]
)
GO

--##SECTION END [CREATE PK]

--##SECTION BEGIN [AUDIT TABLES PK]

--DROP PRIMARY KEY FOR TABLE [__AUDIT__actor]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__ACTOR'))
ALTER TABLE [dbo].[__AUDIT__actor] DROP CONSTRAINT [PK___AUDIT__ACTOR]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__address]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__ADDRESS'))
ALTER TABLE [dbo].[__AUDIT__address] DROP CONSTRAINT [PK___AUDIT__ADDRESS]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__category]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__CATEGORY'))
ALTER TABLE [dbo].[__AUDIT__category] DROP CONSTRAINT [PK___AUDIT__CATEGORY]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__city]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__CITY'))
ALTER TABLE [dbo].[__AUDIT__city] DROP CONSTRAINT [PK___AUDIT__CITY]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__country]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__COUNTRY'))
ALTER TABLE [dbo].[__AUDIT__country] DROP CONSTRAINT [PK___AUDIT__COUNTRY]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__customer]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__CUSTOMER'))
ALTER TABLE [dbo].[__AUDIT__customer] DROP CONSTRAINT [PK___AUDIT__CUSTOMER]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__film]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__FILM'))
ALTER TABLE [dbo].[__AUDIT__film] DROP CONSTRAINT [PK___AUDIT__FILM]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__film_actor]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__FILM_ACTOR'))
ALTER TABLE [dbo].[__AUDIT__film_actor] DROP CONSTRAINT [PK___AUDIT__FILM_ACTOR]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__film_category]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__FILM_CATEGORY'))
ALTER TABLE [dbo].[__AUDIT__film_category] DROP CONSTRAINT [PK___AUDIT__FILM_CATEGORY]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__film_text]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__FILM_TEXT'))
ALTER TABLE [dbo].[__AUDIT__film_text] DROP CONSTRAINT [PK___AUDIT__FILM_TEXT]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__inventory]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__INVENTORY'))
ALTER TABLE [dbo].[__AUDIT__inventory] DROP CONSTRAINT [PK___AUDIT__INVENTORY]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__language]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__LANGUAGE'))
ALTER TABLE [dbo].[__AUDIT__language] DROP CONSTRAINT [PK___AUDIT__LANGUAGE]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__payment]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__PAYMENT'))
ALTER TABLE [dbo].[__AUDIT__payment] DROP CONSTRAINT [PK___AUDIT__PAYMENT]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__rental]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__RENTAL'))
ALTER TABLE [dbo].[__AUDIT__rental] DROP CONSTRAINT [PK___AUDIT__RENTAL]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__staff]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__STAFF'))
ALTER TABLE [dbo].[__AUDIT__staff] DROP CONSTRAINT [PK___AUDIT__STAFF]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__store]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__STORE'))
ALTER TABLE [dbo].[__AUDIT__store] DROP CONSTRAINT [PK___AUDIT__STORE]
GO

--##SECTION END [AUDIT TABLES PK]

--FOREIGN KEY RELATIONSHIP [city] -> [address] ([city].[city_id] -> [address].[city_id])
if not exists(select * from sysobjects where name = 'FK__ADDRESS_CITY' and xtype = 'F')
ALTER TABLE [dbo].[address] ADD 
CONSTRAINT [FK__ADDRESS_CITY] FOREIGN KEY 
(
	[city_id]
) REFERENCES [dbo].[city] (
	[city_id]
)
GO

--FOREIGN KEY RELATIONSHIP [country] -> [city] ([country].[country_id] -> [city].[country_id])
if not exists(select * from sysobjects where name = 'FK__CITY_COUNTRY' and xtype = 'F')
ALTER TABLE [dbo].[city] ADD 
CONSTRAINT [FK__CITY_COUNTRY] FOREIGN KEY 
(
	[country_id]
) REFERENCES [dbo].[country] (
	[country_id]
)
GO

--FOREIGN KEY RELATIONSHIP [address] -> [customer] ([address].[address_id] -> [customer].[address_id])
if not exists(select * from sysobjects where name = 'FK__CUSTOMER_ADDRESS' and xtype = 'F')
ALTER TABLE [dbo].[customer] ADD 
CONSTRAINT [FK__CUSTOMER_ADDRESS] FOREIGN KEY 
(
	[address_id]
) REFERENCES [dbo].[address] (
	[address_id]
)
GO

--FOREIGN KEY RELATIONSHIP [store] -> [customer] ([store].[store_id] -> [customer].[store_id])
if not exists(select * from sysobjects where name = 'FK__CUSTOMER_STORE' and xtype = 'F')
ALTER TABLE [dbo].[customer] ADD 
CONSTRAINT [FK__CUSTOMER_STORE] FOREIGN KEY 
(
	[store_id]
) REFERENCES [dbo].[store] (
	[store_id]
)
GO

--FOREIGN KEY RELATIONSHIP [language] -> [film] ([language].[language_id] -> [film].[language_id])
if not exists(select * from sysobjects where name = 'FK__FILM_LANGUAGE' and xtype = 'F')
ALTER TABLE [dbo].[film] ADD 
CONSTRAINT [FK__FILM_LANGUAGE] FOREIGN KEY 
(
	[language_id]
) REFERENCES [dbo].[language] (
	[language_id]
)
GO

--FOREIGN KEY RELATIONSHIP [language] -> [film] ([language].[language_id] -> [film].[original_language_id])
if not exists(select * from sysobjects where name = 'FK_ORIGINAL_FILM_LANGUAGE' and xtype = 'F')
ALTER TABLE [dbo].[film] ADD 
CONSTRAINT [FK_ORIGINAL_FILM_LANGUAGE] FOREIGN KEY 
(
	[original_language_id]
) REFERENCES [dbo].[language] (
	[language_id]
)
GO

--FOREIGN KEY RELATIONSHIP [actor] -> [film_actor] ([actor].[actor_id] -> [film_actor].[actor_id])
if not exists(select * from sysobjects where name = 'FK__FILM_ACTOR_ACTOR' and xtype = 'F')
ALTER TABLE [dbo].[film_actor] ADD 
CONSTRAINT [FK__FILM_ACTOR_ACTOR] FOREIGN KEY 
(
	[actor_id]
) REFERENCES [dbo].[actor] (
	[actor_id]
)
GO

--FOREIGN KEY RELATIONSHIP [film] -> [film_actor] ([film].[film_id] -> [film_actor].[film_id])
if not exists(select * from sysobjects where name = 'FK__FILM_ACTOR_FILM' and xtype = 'F')
ALTER TABLE [dbo].[film_actor] ADD 
CONSTRAINT [FK__FILM_ACTOR_FILM] FOREIGN KEY 
(
	[film_id]
) REFERENCES [dbo].[film] (
	[film_id]
)
GO

--FOREIGN KEY RELATIONSHIP [category] -> [film_category] ([category].[category_id] -> [film_category].[category_id])
if not exists(select * from sysobjects where name = 'FK__FILM_CATEGORY_CATEGORY' and xtype = 'F')
ALTER TABLE [dbo].[film_category] ADD 
CONSTRAINT [FK__FILM_CATEGORY_CATEGORY] FOREIGN KEY 
(
	[category_id]
) REFERENCES [dbo].[category] (
	[category_id]
)
GO

--FOREIGN KEY RELATIONSHIP [film] -> [film_category] ([film].[film_id] -> [film_category].[film_id])
if not exists(select * from sysobjects where name = 'FK__FILM_CATEGORY_FILM' and xtype = 'F')
ALTER TABLE [dbo].[film_category] ADD 
CONSTRAINT [FK__FILM_CATEGORY_FILM] FOREIGN KEY 
(
	[film_id]
) REFERENCES [dbo].[film] (
	[film_id]
)
GO

--FOREIGN KEY RELATIONSHIP [film] -> [inventory] ([film].[film_id] -> [inventory].[film_id])
if not exists(select * from sysobjects where name = 'FK__INVENTORY_FILM' and xtype = 'F')
ALTER TABLE [dbo].[inventory] ADD 
CONSTRAINT [FK__INVENTORY_FILM] FOREIGN KEY 
(
	[film_id]
) REFERENCES [dbo].[film] (
	[film_id]
)
GO

--FOREIGN KEY RELATIONSHIP [store] -> [inventory] ([store].[store_id] -> [inventory].[store_id])
if not exists(select * from sysobjects where name = 'FK__INVENTORY_STORE' and xtype = 'F')
ALTER TABLE [dbo].[inventory] ADD 
CONSTRAINT [FK__INVENTORY_STORE] FOREIGN KEY 
(
	[store_id]
) REFERENCES [dbo].[store] (
	[store_id]
)
GO

--FOREIGN KEY RELATIONSHIP [customer] -> [payment] ([customer].[customer_id] -> [payment].[customer_id])
if not exists(select * from sysobjects where name = 'FK__PAYMENT_CUSTOMER' and xtype = 'F')
ALTER TABLE [dbo].[payment] ADD 
CONSTRAINT [FK__PAYMENT_CUSTOMER] FOREIGN KEY 
(
	[customer_id]
) REFERENCES [dbo].[customer] (
	[customer_id]
)
GO

--FOREIGN KEY RELATIONSHIP [rental] -> [payment] ([rental].[rental_id] -> [payment].[rental_id])
if not exists(select * from sysobjects where name = 'FK__PAYMENT_RENTAL' and xtype = 'F')
ALTER TABLE [dbo].[payment] ADD 
CONSTRAINT [FK__PAYMENT_RENTAL] FOREIGN KEY 
(
	[rental_id]
) REFERENCES [dbo].[rental] (
	[rental_id]
)
GO

--FOREIGN KEY RELATIONSHIP [staff] -> [payment] ([staff].[staff_id] -> [payment].[staff_id])
if not exists(select * from sysobjects where name = 'FK__PAYMENT_STAFF' and xtype = 'F')
ALTER TABLE [dbo].[payment] ADD 
CONSTRAINT [FK__PAYMENT_STAFF] FOREIGN KEY 
(
	[staff_id]
) REFERENCES [dbo].[staff] (
	[staff_id]
)
GO

--FOREIGN KEY RELATIONSHIP [customer] -> [rental] ([customer].[customer_id] -> [rental].[customer_id])
if not exists(select * from sysobjects where name = 'FK__RENTAL_CUSTOMER' and xtype = 'F')
ALTER TABLE [dbo].[rental] ADD 
CONSTRAINT [FK__RENTAL_CUSTOMER] FOREIGN KEY 
(
	[customer_id]
) REFERENCES [dbo].[customer] (
	[customer_id]
)
GO

--FOREIGN KEY RELATIONSHIP [inventory] -> [rental] ([inventory].[inventory_id] -> [rental].[inventory_id])
if not exists(select * from sysobjects where name = 'FK__RENTAL_INVENTORY' and xtype = 'F')
ALTER TABLE [dbo].[rental] ADD 
CONSTRAINT [FK__RENTAL_INVENTORY] FOREIGN KEY 
(
	[inventory_id]
) REFERENCES [dbo].[inventory] (
	[inventory_id]
)
GO

--FOREIGN KEY RELATIONSHIP [staff] -> [rental] ([staff].[staff_id] -> [rental].[staff_id])
if not exists(select * from sysobjects where name = 'FK__RENTAL_STAFF' and xtype = 'F')
ALTER TABLE [dbo].[rental] ADD 
CONSTRAINT [FK__RENTAL_STAFF] FOREIGN KEY 
(
	[staff_id]
) REFERENCES [dbo].[staff] (
	[staff_id]
)
GO

--FOREIGN KEY RELATIONSHIP [address] -> [staff] ([address].[address_id] -> [staff].[address_id])
if not exists(select * from sysobjects where name = 'FK__STAFF_ADDRESS' and xtype = 'F')
ALTER TABLE [dbo].[staff] ADD 
CONSTRAINT [FK__STAFF_ADDRESS] FOREIGN KEY 
(
	[address_id]
) REFERENCES [dbo].[address] (
	[address_id]
)
GO

--FOREIGN KEY RELATIONSHIP [store] -> [staff] ([store].[store_id] -> [staff].[store_id])
if not exists(select * from sysobjects where name = 'FK__STAFF_STORE' and xtype = 'F')
ALTER TABLE [dbo].[staff] ADD 
CONSTRAINT [FK__STAFF_STORE] FOREIGN KEY 
(
	[store_id]
) REFERENCES [dbo].[store] (
	[store_id]
)
GO

--FOREIGN KEY RELATIONSHIP [address] -> [store] ([address].[address_id] -> [store].[address_id])
if not exists(select * from sysobjects where name = 'FK__STORE_ADDRESS' and xtype = 'F')
ALTER TABLE [dbo].[store] ADD 
CONSTRAINT [FK__STORE_ADDRESS] FOREIGN KEY 
(
	[address_id]
) REFERENCES [dbo].[address] (
	[address_id]
)
GO

--FOREIGN KEY RELATIONSHIP [staff] -> [store] ([staff].[staff_id] -> [store].[manager_staff_id])
if not exists(select * from sysobjects where name = 'FK_MANAGER_STORE_STAFF' and xtype = 'F')
ALTER TABLE [dbo].[store] ADD 
CONSTRAINT [FK_MANAGER_STORE_STAFF] FOREIGN KEY 
(
	[manager_staff_id]
) REFERENCES [dbo].[staff] (
	[staff_id]
)
GO

--INDEX FOR TABLE [actor] COLUMNS: [last_name]
if not exists(select * from sys.indexes where name = 'IDX_ACTOR_LAST_NAME')
CREATE NONCLUSTERED INDEX [IDX_ACTOR_LAST_NAME] ON [dbo].[actor] ([last_name] ASC)

GO

--INDEX FOR TABLE [address] COLUMNS: [city_id]
if not exists(select * from sys.indexes where name = 'IDX_ADDRESS_CITY_ID')
CREATE NONCLUSTERED INDEX [IDX_ADDRESS_CITY_ID] ON [dbo].[address] ([city_id] ASC)

GO

--INDEX FOR TABLE [city] COLUMNS: [country_id]
if not exists(select * from sys.indexes where name = 'IDX_CITY_COUNTRY_ID')
CREATE NONCLUSTERED INDEX [IDX_CITY_COUNTRY_ID] ON [dbo].[city] ([country_id] ASC)

GO

--INDEX FOR TABLE [customer] COLUMNS: [store_id]
if not exists(select * from sys.indexes where name = 'IDX_CUSTOMER_STORE_ID')
CREATE NONCLUSTERED INDEX [IDX_CUSTOMER_STORE_ID] ON [dbo].[customer] ([store_id] ASC)

GO

--INDEX FOR TABLE [customer] COLUMNS: [address_id]
if not exists(select * from sys.indexes where name = 'IDX_CUSTOMER_ADDRESS_ID')
CREATE NONCLUSTERED INDEX [IDX_CUSTOMER_ADDRESS_ID] ON [dbo].[customer] ([address_id] ASC)

GO

--INDEX FOR TABLE [customer] COLUMNS: [last_name]
if not exists(select * from sys.indexes where name = 'IDX_CUSTOMER_LAST_NAME')
CREATE NONCLUSTERED INDEX [IDX_CUSTOMER_LAST_NAME] ON [dbo].[customer] ([last_name] ASC)

GO

--INDEX FOR TABLE [film] COLUMNS: [title]
if not exists(select * from sys.indexes where name = 'IDX_FILM_TITLE')
CREATE NONCLUSTERED INDEX [IDX_FILM_TITLE] ON [dbo].[film] ([title] ASC)

GO

--INDEX FOR TABLE [film] COLUMNS: [language_id]
if not exists(select * from sys.indexes where name = 'IDX_FILM_LANGUAGE_ID')
CREATE NONCLUSTERED INDEX [IDX_FILM_LANGUAGE_ID] ON [dbo].[film] ([language_id] ASC)

GO

--INDEX FOR TABLE [film] COLUMNS: [original_language_id]
if not exists(select * from sys.indexes where name = 'IDX_FILM_ORIGINAL_LANGUAGE_ID')
CREATE NONCLUSTERED INDEX [IDX_FILM_ORIGINAL_LANGUAGE_ID] ON [dbo].[film] ([original_language_id] ASC)

GO

--INDEX FOR TABLE [film_actor] COLUMNS: [film_id]
if not exists(select * from sys.indexes where name = 'IDX_FILM_ACTOR_FILM_ID')
CREATE NONCLUSTERED INDEX [IDX_FILM_ACTOR_FILM_ID] ON [dbo].[film_actor] ([film_id] ASC)

GO

--INDEX FOR TABLE [film_category] COLUMNS: [category_id]
if not exists(select * from sys.indexes where name = 'IDX_FILM_CATEGORY_CATEGORY_ID')
CREATE NONCLUSTERED INDEX [IDX_FILM_CATEGORY_CATEGORY_ID] ON [dbo].[film_category] ([category_id] ASC)

GO

--INDEX FOR TABLE [inventory] COLUMNS: [film_id]
if not exists(select * from sys.indexes where name = 'IDX_INVENTORY_FILM_ID')
CREATE NONCLUSTERED INDEX [IDX_INVENTORY_FILM_ID] ON [dbo].[inventory] ([film_id] ASC)

GO

--INDEX FOR TABLE [inventory] COLUMNS: [store_id],  [film_id]
if not exists(select * from sys.indexes where name = 'IDX_INVENTORY_STORE_ID_FILM_ID')
CREATE NONCLUSTERED INDEX [IDX_INVENTORY_STORE_ID_FILM_ID] ON [dbo].[inventory] ([store_id] ASC,[film_id] ASC)

GO

--INDEX FOR TABLE [payment] COLUMNS: [staff_id]
if not exists(select * from sys.indexes where name = 'IDX_PAYMENT_STAFF_ID')
CREATE NONCLUSTERED INDEX [IDX_PAYMENT_STAFF_ID] ON [dbo].[payment] ([staff_id] ASC)

GO

--INDEX FOR TABLE [payment] COLUMNS: [customer_id]
if not exists(select * from sys.indexes where name = 'IDX_PAYMENT_CUSTOMER_ID')
CREATE NONCLUSTERED INDEX [IDX_PAYMENT_CUSTOMER_ID] ON [dbo].[payment] ([customer_id] ASC)

GO

--INDEX FOR TABLE [payment] COLUMNS: [rental_id]
if not exists(select * from sys.indexes where name = 'IDX_PAYMENT_RENTAL_ID')
CREATE NONCLUSTERED INDEX [IDX_PAYMENT_RENTAL_ID] ON [dbo].[payment] ([rental_id] ASC)

GO

--INDEX FOR TABLE [rental] COLUMNS: [rental_date],  [inventory_id],  [customer_id]
if not exists(select * from sys.indexes where name = 'IDX_RENTAL_RENTAL_DATE_INVENTORY_ID_CUSTOMER_ID')
CREATE UNIQUE NONCLUSTERED INDEX [IDX_RENTAL_RENTAL_DATE_INVENTORY_ID_CUSTOMER_ID] ON [dbo].[rental] ([rental_date] ASC,[inventory_id] ASC,[customer_id] ASC)

GO

--INDEX FOR TABLE [rental] COLUMNS: [inventory_id]
if not exists(select * from sys.indexes where name = 'IDX_RENTAL_INVENTORY_ID')
CREATE NONCLUSTERED INDEX [IDX_RENTAL_INVENTORY_ID] ON [dbo].[rental] ([inventory_id] ASC)

GO

--INDEX FOR TABLE [rental] COLUMNS: [customer_id]
if not exists(select * from sys.indexes where name = 'IDX_RENTAL_CUSTOMER_ID')
CREATE NONCLUSTERED INDEX [IDX_RENTAL_CUSTOMER_ID] ON [dbo].[rental] ([customer_id] ASC)

GO

--INDEX FOR TABLE [rental] COLUMNS: [staff_id]
if not exists(select * from sys.indexes where name = 'IDX_RENTAL_STAFF_ID')
CREATE NONCLUSTERED INDEX [IDX_RENTAL_STAFF_ID] ON [dbo].[rental] ([staff_id] ASC)

GO

--INDEX FOR TABLE [staff] COLUMNS: [store_id]
if not exists(select * from sys.indexes where name = 'IDX_STAFF_STORE_ID')
CREATE NONCLUSTERED INDEX [IDX_STAFF_STORE_ID] ON [dbo].[staff] ([store_id] ASC)

GO

--INDEX FOR TABLE [staff] COLUMNS: [address_id]
if not exists(select * from sys.indexes where name = 'IDX_STAFF_ADDRESS_ID')
CREATE NONCLUSTERED INDEX [IDX_STAFF_ADDRESS_ID] ON [dbo].[staff] ([address_id] ASC)

GO

--INDEX FOR TABLE [store] COLUMNS: [manager_staff_id]
if not exists(select * from sys.indexes where name = 'IDX_STORE_MANAGER_STAFF_ID')
CREATE UNIQUE NONCLUSTERED INDEX [IDX_STORE_MANAGER_STAFF_ID] ON [dbo].[store] ([manager_staff_id] ASC)

GO

--INDEX FOR TABLE [store] COLUMNS: [address_id]
if not exists(select * from sys.indexes where name = 'IDX_STORE_ADDRESS_ID')
CREATE NONCLUSTERED INDEX [IDX_STORE_ADDRESS_ID] ON [dbo].[store] ([address_id] ASC)

GO

--##SECTION BEGIN [REMOVE DEFAULTS]

--BEGIN DEFAULTS FOR TABLE [actor]
--DROP CONSTRAINT FOR '[actor].[actor_id]' if one exists
declare @actor_actor_id varchar(500)
set @actor_actor_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='actor' and a.name = 'actor_id')
if (@actor_actor_id IS NOT NULL) exec ('ALTER TABLE [actor] DROP CONSTRAINT [' + @actor_actor_id + ']')

--DROP CONSTRAINT FOR '[actor].[first_name]' if one exists
declare @actor_first_name varchar(500)
set @actor_first_name = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='actor' and a.name = 'first_name')
if (@actor_first_name IS NOT NULL) exec ('ALTER TABLE [actor] DROP CONSTRAINT [' + @actor_first_name + ']')

--DROP CONSTRAINT FOR '[actor].[last_name]' if one exists
declare @actor_last_name varchar(500)
set @actor_last_name = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='actor' and a.name = 'last_name')
if (@actor_last_name IS NOT NULL) exec ('ALTER TABLE [actor] DROP CONSTRAINT [' + @actor_last_name + ']')

--END DEFAULTS FOR TABLE [actor]
GO

--BEGIN DEFAULTS FOR TABLE [address]
--DROP CONSTRAINT FOR '[address].[address]' if one exists
declare @address_address1 varchar(500)
set @address_address1 = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='address' and a.name = 'address')
if (@address_address1 IS NOT NULL) exec ('ALTER TABLE [address] DROP CONSTRAINT [' + @address_address1 + ']')

--DROP CONSTRAINT FOR '[address].[address_id]' if one exists
declare @address_address_id varchar(500)
set @address_address_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='address' and a.name = 'address_id')
if (@address_address_id IS NOT NULL) exec ('ALTER TABLE [address] DROP CONSTRAINT [' + @address_address_id + ']')

--DROP CONSTRAINT FOR '[address].[address2]' if one exists
declare @address_address2 varchar(500)
set @address_address2 = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='address' and a.name = 'address2')
if (@address_address2 IS NOT NULL) exec ('ALTER TABLE [address] DROP CONSTRAINT [' + @address_address2 + ']')

--DROP CONSTRAINT FOR '[address].[city_id]' if one exists
declare @address_city_id varchar(500)
set @address_city_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='address' and a.name = 'city_id')
if (@address_city_id IS NOT NULL) exec ('ALTER TABLE [address] DROP CONSTRAINT [' + @address_city_id + ']')

--DROP CONSTRAINT FOR '[address].[district]' if one exists
declare @address_district varchar(500)
set @address_district = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='address' and a.name = 'district')
if (@address_district IS NOT NULL) exec ('ALTER TABLE [address] DROP CONSTRAINT [' + @address_district + ']')

--DROP CONSTRAINT FOR '[address].[phone]' if one exists
declare @address_phone varchar(500)
set @address_phone = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='address' and a.name = 'phone')
if (@address_phone IS NOT NULL) exec ('ALTER TABLE [address] DROP CONSTRAINT [' + @address_phone + ']')

--DROP CONSTRAINT FOR '[address].[postal_code]' if one exists
declare @address_postal_code varchar(500)
set @address_postal_code = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='address' and a.name = 'postal_code')
if (@address_postal_code IS NOT NULL) exec ('ALTER TABLE [address] DROP CONSTRAINT [' + @address_postal_code + ']')

--END DEFAULTS FOR TABLE [address]
GO

--BEGIN DEFAULTS FOR TABLE [category]
--DROP CONSTRAINT FOR '[category].[category_id]' if one exists
declare @category_category_id varchar(500)
set @category_category_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='category' and a.name = 'category_id')
if (@category_category_id IS NOT NULL) exec ('ALTER TABLE [category] DROP CONSTRAINT [' + @category_category_id + ']')

--DROP CONSTRAINT FOR '[category].[name]' if one exists
declare @category_name varchar(500)
set @category_name = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='category' and a.name = 'name')
if (@category_name IS NOT NULL) exec ('ALTER TABLE [category] DROP CONSTRAINT [' + @category_name + ']')

--END DEFAULTS FOR TABLE [category]
GO

--BEGIN DEFAULTS FOR TABLE [city]
--DROP CONSTRAINT FOR '[city].[city]' if one exists
declare @city_name varchar(500)
set @city_name = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='city' and a.name = 'city')
if (@city_name IS NOT NULL) exec ('ALTER TABLE [city] DROP CONSTRAINT [' + @city_name + ']')

--DROP CONSTRAINT FOR '[city].[city_id]' if one exists
declare @city_city_id varchar(500)
set @city_city_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='city' and a.name = 'city_id')
if (@city_city_id IS NOT NULL) exec ('ALTER TABLE [city] DROP CONSTRAINT [' + @city_city_id + ']')

--DROP CONSTRAINT FOR '[city].[country_id]' if one exists
declare @city_country_id varchar(500)
set @city_country_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='city' and a.name = 'country_id')
if (@city_country_id IS NOT NULL) exec ('ALTER TABLE [city] DROP CONSTRAINT [' + @city_country_id + ']')

--END DEFAULTS FOR TABLE [city]
GO

--BEGIN DEFAULTS FOR TABLE [country]
--DROP CONSTRAINT FOR '[country].[country]' if one exists
declare @country_name varchar(500)
set @country_name = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='country' and a.name = 'country')
if (@country_name IS NOT NULL) exec ('ALTER TABLE [country] DROP CONSTRAINT [' + @country_name + ']')

--DROP CONSTRAINT FOR '[country].[country_id]' if one exists
declare @country_country_id varchar(500)
set @country_country_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='country' and a.name = 'country_id')
if (@country_country_id IS NOT NULL) exec ('ALTER TABLE [country] DROP CONSTRAINT [' + @country_country_id + ']')

--END DEFAULTS FOR TABLE [country]
GO

--BEGIN DEFAULTS FOR TABLE [customer]
--DROP CONSTRAINT FOR '[customer].[active]' if one exists
declare @customer_active varchar(500)
set @customer_active = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='customer' and a.name = 'active')
if (@customer_active IS NOT NULL) exec ('ALTER TABLE [customer] DROP CONSTRAINT [' + @customer_active + ']')

--DROP CONSTRAINT FOR '[customer].[address_id]' if one exists
declare @customer_address_id varchar(500)
set @customer_address_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='customer' and a.name = 'address_id')
if (@customer_address_id IS NOT NULL) exec ('ALTER TABLE [customer] DROP CONSTRAINT [' + @customer_address_id + ']')

--DROP CONSTRAINT FOR '[customer].[customer_id]' if one exists
declare @customer_customer_id varchar(500)
set @customer_customer_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='customer' and a.name = 'customer_id')
if (@customer_customer_id IS NOT NULL) exec ('ALTER TABLE [customer] DROP CONSTRAINT [' + @customer_customer_id + ']')

--DROP CONSTRAINT FOR '[customer].[email]' if one exists
declare @customer_email varchar(500)
set @customer_email = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='customer' and a.name = 'email')
if (@customer_email IS NOT NULL) exec ('ALTER TABLE [customer] DROP CONSTRAINT [' + @customer_email + ']')

--DROP CONSTRAINT FOR '[customer].[first_name]' if one exists
declare @customer_first_name varchar(500)
set @customer_first_name = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='customer' and a.name = 'first_name')
if (@customer_first_name IS NOT NULL) exec ('ALTER TABLE [customer] DROP CONSTRAINT [' + @customer_first_name + ']')

--DROP CONSTRAINT FOR '[customer].[last_name]' if one exists
declare @customer_last_name varchar(500)
set @customer_last_name = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='customer' and a.name = 'last_name')
if (@customer_last_name IS NOT NULL) exec ('ALTER TABLE [customer] DROP CONSTRAINT [' + @customer_last_name + ']')

--DROP CONSTRAINT FOR '[customer].[store_id]' if one exists
declare @customer_store_id varchar(500)
set @customer_store_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='customer' and a.name = 'store_id')
if (@customer_store_id IS NOT NULL) exec ('ALTER TABLE [customer] DROP CONSTRAINT [' + @customer_store_id + ']')

--END DEFAULTS FOR TABLE [customer]
GO

--BEGIN DEFAULTS FOR TABLE [film]
--DROP CONSTRAINT FOR '[film].[description]' if one exists
declare @film_description varchar(500)
set @film_description = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='film' and a.name = 'description')
if (@film_description IS NOT NULL) exec ('ALTER TABLE [film] DROP CONSTRAINT [' + @film_description + ']')

--DROP CONSTRAINT FOR '[film].[film_id]' if one exists
declare @film_film_id varchar(500)
set @film_film_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='film' and a.name = 'film_id')
if (@film_film_id IS NOT NULL) exec ('ALTER TABLE [film] DROP CONSTRAINT [' + @film_film_id + ']')

--DROP CONSTRAINT FOR '[film].[language_id]' if one exists
declare @film_language_id varchar(500)
set @film_language_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='film' and a.name = 'language_id')
if (@film_language_id IS NOT NULL) exec ('ALTER TABLE [film] DROP CONSTRAINT [' + @film_language_id + ']')

--DROP CONSTRAINT FOR '[film].[length]' if one exists
declare @film_length varchar(500)
set @film_length = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='film' and a.name = 'length')
if (@film_length IS NOT NULL) exec ('ALTER TABLE [film] DROP CONSTRAINT [' + @film_length + ']')

--DROP CONSTRAINT FOR '[film].[original_language_id]' if one exists
declare @film_original_language_id varchar(500)
set @film_original_language_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='film' and a.name = 'original_language_id')
if (@film_original_language_id IS NOT NULL) exec ('ALTER TABLE [film] DROP CONSTRAINT [' + @film_original_language_id + ']')

--DROP CONSTRAINT FOR '[film].[rating]' if one exists
declare @film_rating varchar(500)
set @film_rating = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='film' and a.name = 'rating')
if (@film_rating IS NOT NULL) exec ('ALTER TABLE [film] DROP CONSTRAINT [' + @film_rating + ']')

--DROP CONSTRAINT FOR '[film].[release_year]' if one exists
declare @film_release_year varchar(500)
set @film_release_year = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='film' and a.name = 'release_year')
if (@film_release_year IS NOT NULL) exec ('ALTER TABLE [film] DROP CONSTRAINT [' + @film_release_year + ']')

--DROP CONSTRAINT FOR '[film].[rental_duration]' if one exists
declare @film_rental_duration varchar(500)
set @film_rental_duration = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='film' and a.name = 'rental_duration')
if (@film_rental_duration IS NOT NULL) exec ('ALTER TABLE [film] DROP CONSTRAINT [' + @film_rental_duration + ']')

--DROP CONSTRAINT FOR '[film].[rental_rate]' if one exists
declare @film_rental_rate varchar(500)
set @film_rental_rate = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='film' and a.name = 'rental_rate')
if (@film_rental_rate IS NOT NULL) exec ('ALTER TABLE [film] DROP CONSTRAINT [' + @film_rental_rate + ']')

--DROP CONSTRAINT FOR '[film].[replacement_cost]' if one exists
declare @film_replacement_cost varchar(500)
set @film_replacement_cost = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='film' and a.name = 'replacement_cost')
if (@film_replacement_cost IS NOT NULL) exec ('ALTER TABLE [film] DROP CONSTRAINT [' + @film_replacement_cost + ']')

--DROP CONSTRAINT FOR '[film].[special_features]' if one exists
declare @film_special_features varchar(500)
set @film_special_features = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='film' and a.name = 'special_features')
if (@film_special_features IS NOT NULL) exec ('ALTER TABLE [film] DROP CONSTRAINT [' + @film_special_features + ']')

--DROP CONSTRAINT FOR '[film].[title]' if one exists
declare @film_title varchar(500)
set @film_title = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='film' and a.name = 'title')
if (@film_title IS NOT NULL) exec ('ALTER TABLE [film] DROP CONSTRAINT [' + @film_title + ']')

--END DEFAULTS FOR TABLE [film]
GO

--BEGIN DEFAULTS FOR TABLE [film_actor]
--DROP CONSTRAINT FOR '[film_actor].[actor_id]' if one exists
declare @film_actor_actor_id varchar(500)
set @film_actor_actor_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='film_actor' and a.name = 'actor_id')
if (@film_actor_actor_id IS NOT NULL) exec ('ALTER TABLE [film_actor] DROP CONSTRAINT [' + @film_actor_actor_id + ']')

--DROP CONSTRAINT FOR '[film_actor].[film_id]' if one exists
declare @film_actor_film_id varchar(500)
set @film_actor_film_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='film_actor' and a.name = 'film_id')
if (@film_actor_film_id IS NOT NULL) exec ('ALTER TABLE [film_actor] DROP CONSTRAINT [' + @film_actor_film_id + ']')

--END DEFAULTS FOR TABLE [film_actor]
GO

--BEGIN DEFAULTS FOR TABLE [film_category]
--DROP CONSTRAINT FOR '[film_category].[category_id]' if one exists
declare @film_category_category_id varchar(500)
set @film_category_category_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='film_category' and a.name = 'category_id')
if (@film_category_category_id IS NOT NULL) exec ('ALTER TABLE [film_category] DROP CONSTRAINT [' + @film_category_category_id + ']')

--DROP CONSTRAINT FOR '[film_category].[film_id]' if one exists
declare @film_category_film_id varchar(500)
set @film_category_film_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='film_category' and a.name = 'film_id')
if (@film_category_film_id IS NOT NULL) exec ('ALTER TABLE [film_category] DROP CONSTRAINT [' + @film_category_film_id + ']')

--END DEFAULTS FOR TABLE [film_category]
GO

--BEGIN DEFAULTS FOR TABLE [film_text]
--DROP CONSTRAINT FOR '[film_text].[description]' if one exists
declare @film_text_description varchar(500)
set @film_text_description = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='film_text' and a.name = 'description')
if (@film_text_description IS NOT NULL) exec ('ALTER TABLE [film_text] DROP CONSTRAINT [' + @film_text_description + ']')

--DROP CONSTRAINT FOR '[film_text].[film_id]' if one exists
declare @film_text_film_id varchar(500)
set @film_text_film_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='film_text' and a.name = 'film_id')
if (@film_text_film_id IS NOT NULL) exec ('ALTER TABLE [film_text] DROP CONSTRAINT [' + @film_text_film_id + ']')

--DROP CONSTRAINT FOR '[film_text].[title]' if one exists
declare @film_text_title varchar(500)
set @film_text_title = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='film_text' and a.name = 'title')
if (@film_text_title IS NOT NULL) exec ('ALTER TABLE [film_text] DROP CONSTRAINT [' + @film_text_title + ']')

--END DEFAULTS FOR TABLE [film_text]
GO

--BEGIN DEFAULTS FOR TABLE [inventory]
--DROP CONSTRAINT FOR '[inventory].[film_id]' if one exists
declare @inventory_film_id varchar(500)
set @inventory_film_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='inventory' and a.name = 'film_id')
if (@inventory_film_id IS NOT NULL) exec ('ALTER TABLE [inventory] DROP CONSTRAINT [' + @inventory_film_id + ']')

--DROP CONSTRAINT FOR '[inventory].[inventory_id]' if one exists
declare @inventory_inventory_id varchar(500)
set @inventory_inventory_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='inventory' and a.name = 'inventory_id')
if (@inventory_inventory_id IS NOT NULL) exec ('ALTER TABLE [inventory] DROP CONSTRAINT [' + @inventory_inventory_id + ']')

--DROP CONSTRAINT FOR '[inventory].[store_id]' if one exists
declare @inventory_store_id varchar(500)
set @inventory_store_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='inventory' and a.name = 'store_id')
if (@inventory_store_id IS NOT NULL) exec ('ALTER TABLE [inventory] DROP CONSTRAINT [' + @inventory_store_id + ']')

--END DEFAULTS FOR TABLE [inventory]
GO

--BEGIN DEFAULTS FOR TABLE [language]
--DROP CONSTRAINT FOR '[language].[language_id]' if one exists
declare @language_language_id varchar(500)
set @language_language_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='language' and a.name = 'language_id')
if (@language_language_id IS NOT NULL) exec ('ALTER TABLE [language] DROP CONSTRAINT [' + @language_language_id + ']')

--DROP CONSTRAINT FOR '[language].[name]' if one exists
declare @language_name varchar(500)
set @language_name = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='language' and a.name = 'name')
if (@language_name IS NOT NULL) exec ('ALTER TABLE [language] DROP CONSTRAINT [' + @language_name + ']')

--END DEFAULTS FOR TABLE [language]
GO

--BEGIN DEFAULTS FOR TABLE [payment]
--DROP CONSTRAINT FOR '[payment].[amount]' if one exists
declare @payment_amount varchar(500)
set @payment_amount = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='payment' and a.name = 'amount')
if (@payment_amount IS NOT NULL) exec ('ALTER TABLE [payment] DROP CONSTRAINT [' + @payment_amount + ']')

--DROP CONSTRAINT FOR '[payment].[customer_id]' if one exists
declare @payment_customer_id varchar(500)
set @payment_customer_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='payment' and a.name = 'customer_id')
if (@payment_customer_id IS NOT NULL) exec ('ALTER TABLE [payment] DROP CONSTRAINT [' + @payment_customer_id + ']')

--DROP CONSTRAINT FOR '[payment].[payment_date]' if one exists
declare @payment_payment_date varchar(500)
set @payment_payment_date = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='payment' and a.name = 'payment_date')
if (@payment_payment_date IS NOT NULL) exec ('ALTER TABLE [payment] DROP CONSTRAINT [' + @payment_payment_date + ']')

--DROP CONSTRAINT FOR '[payment].[payment_id]' if one exists
declare @payment_payment_id varchar(500)
set @payment_payment_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='payment' and a.name = 'payment_id')
if (@payment_payment_id IS NOT NULL) exec ('ALTER TABLE [payment] DROP CONSTRAINT [' + @payment_payment_id + ']')

--DROP CONSTRAINT FOR '[payment].[rental_id]' if one exists
declare @payment_rental_id varchar(500)
set @payment_rental_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='payment' and a.name = 'rental_id')
if (@payment_rental_id IS NOT NULL) exec ('ALTER TABLE [payment] DROP CONSTRAINT [' + @payment_rental_id + ']')

--DROP CONSTRAINT FOR '[payment].[staff_id]' if one exists
declare @payment_staff_id varchar(500)
set @payment_staff_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='payment' and a.name = 'staff_id')
if (@payment_staff_id IS NOT NULL) exec ('ALTER TABLE [payment] DROP CONSTRAINT [' + @payment_staff_id + ']')

--END DEFAULTS FOR TABLE [payment]
GO

--BEGIN DEFAULTS FOR TABLE [rental]
--DROP CONSTRAINT FOR '[rental].[customer_id]' if one exists
declare @rental_customer_id varchar(500)
set @rental_customer_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='rental' and a.name = 'customer_id')
if (@rental_customer_id IS NOT NULL) exec ('ALTER TABLE [rental] DROP CONSTRAINT [' + @rental_customer_id + ']')

--DROP CONSTRAINT FOR '[rental].[inventory_id]' if one exists
declare @rental_inventory_id varchar(500)
set @rental_inventory_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='rental' and a.name = 'inventory_id')
if (@rental_inventory_id IS NOT NULL) exec ('ALTER TABLE [rental] DROP CONSTRAINT [' + @rental_inventory_id + ']')

--DROP CONSTRAINT FOR '[rental].[rental_date]' if one exists
declare @rental_rental_date varchar(500)
set @rental_rental_date = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='rental' and a.name = 'rental_date')
if (@rental_rental_date IS NOT NULL) exec ('ALTER TABLE [rental] DROP CONSTRAINT [' + @rental_rental_date + ']')

--DROP CONSTRAINT FOR '[rental].[rental_id]' if one exists
declare @rental_rental_id varchar(500)
set @rental_rental_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='rental' and a.name = 'rental_id')
if (@rental_rental_id IS NOT NULL) exec ('ALTER TABLE [rental] DROP CONSTRAINT [' + @rental_rental_id + ']')

--DROP CONSTRAINT FOR '[rental].[return_date]' if one exists
declare @rental_return_date varchar(500)
set @rental_return_date = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='rental' and a.name = 'return_date')
if (@rental_return_date IS NOT NULL) exec ('ALTER TABLE [rental] DROP CONSTRAINT [' + @rental_return_date + ']')

--DROP CONSTRAINT FOR '[rental].[staff_id]' if one exists
declare @rental_staff_id varchar(500)
set @rental_staff_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='rental' and a.name = 'staff_id')
if (@rental_staff_id IS NOT NULL) exec ('ALTER TABLE [rental] DROP CONSTRAINT [' + @rental_staff_id + ']')

--END DEFAULTS FOR TABLE [rental]
GO

--BEGIN DEFAULTS FOR TABLE [staff]
--DROP CONSTRAINT FOR '[staff].[active]' if one exists
declare @staff_active varchar(500)
set @staff_active = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='staff' and a.name = 'active')
if (@staff_active IS NOT NULL) exec ('ALTER TABLE [staff] DROP CONSTRAINT [' + @staff_active + ']')

--DROP CONSTRAINT FOR '[staff].[address_id]' if one exists
declare @staff_address_id varchar(500)
set @staff_address_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='staff' and a.name = 'address_id')
if (@staff_address_id IS NOT NULL) exec ('ALTER TABLE [staff] DROP CONSTRAINT [' + @staff_address_id + ']')

--DROP CONSTRAINT FOR '[staff].[email]' if one exists
declare @staff_email varchar(500)
set @staff_email = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='staff' and a.name = 'email')
if (@staff_email IS NOT NULL) exec ('ALTER TABLE [staff] DROP CONSTRAINT [' + @staff_email + ']')

--DROP CONSTRAINT FOR '[staff].[first_name]' if one exists
declare @staff_first_name varchar(500)
set @staff_first_name = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='staff' and a.name = 'first_name')
if (@staff_first_name IS NOT NULL) exec ('ALTER TABLE [staff] DROP CONSTRAINT [' + @staff_first_name + ']')

--DROP CONSTRAINT FOR '[staff].[last_name]' if one exists
declare @staff_last_name varchar(500)
set @staff_last_name = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='staff' and a.name = 'last_name')
if (@staff_last_name IS NOT NULL) exec ('ALTER TABLE [staff] DROP CONSTRAINT [' + @staff_last_name + ']')

--DROP CONSTRAINT FOR '[staff].[password]' if one exists
declare @staff_password varchar(500)
set @staff_password = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='staff' and a.name = 'password')
if (@staff_password IS NOT NULL) exec ('ALTER TABLE [staff] DROP CONSTRAINT [' + @staff_password + ']')

--DROP CONSTRAINT FOR '[staff].[picture]' if one exists
declare @staff_picture varchar(500)
set @staff_picture = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='staff' and a.name = 'picture')
if (@staff_picture IS NOT NULL) exec ('ALTER TABLE [staff] DROP CONSTRAINT [' + @staff_picture + ']')

--DROP CONSTRAINT FOR '[staff].[staff_id]' if one exists
declare @staff_staff_id varchar(500)
set @staff_staff_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='staff' and a.name = 'staff_id')
if (@staff_staff_id IS NOT NULL) exec ('ALTER TABLE [staff] DROP CONSTRAINT [' + @staff_staff_id + ']')

--DROP CONSTRAINT FOR '[staff].[store_id]' if one exists
declare @staff_store_id varchar(500)
set @staff_store_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='staff' and a.name = 'store_id')
if (@staff_store_id IS NOT NULL) exec ('ALTER TABLE [staff] DROP CONSTRAINT [' + @staff_store_id + ']')

--DROP CONSTRAINT FOR '[staff].[username]' if one exists
declare @staff_username varchar(500)
set @staff_username = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='staff' and a.name = 'username')
if (@staff_username IS NOT NULL) exec ('ALTER TABLE [staff] DROP CONSTRAINT [' + @staff_username + ']')

--END DEFAULTS FOR TABLE [staff]
GO

--BEGIN DEFAULTS FOR TABLE [store]
--DROP CONSTRAINT FOR '[store].[address_id]' if one exists
declare @store_address_id varchar(500)
set @store_address_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='store' and a.name = 'address_id')
if (@store_address_id IS NOT NULL) exec ('ALTER TABLE [store] DROP CONSTRAINT [' + @store_address_id + ']')

--DROP CONSTRAINT FOR '[store].[manager_staff_id]' if one exists
declare @store_manager_staff_id varchar(500)
set @store_manager_staff_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='store' and a.name = 'manager_staff_id')
if (@store_manager_staff_id IS NOT NULL) exec ('ALTER TABLE [store] DROP CONSTRAINT [' + @store_manager_staff_id + ']')

--DROP CONSTRAINT FOR '[store].[store_id]' if one exists
declare @store_store_id varchar(500)
set @store_store_id = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='store' and a.name = 'store_id')
if (@store_store_id IS NOT NULL) exec ('ALTER TABLE [store] DROP CONSTRAINT [' + @store_store_id + ']')

--END DEFAULTS FOR TABLE [store]
GO

--##SECTION END [REMOVE DEFAULTS]

--##SECTION BEGIN [CREATE DEFAULTS]

--BEGIN DEFAULTS FOR TABLE [customer]
--ADD CONSTRAINT FOR '[customer].[active]'
if not exists(select * from sysobjects where name = 'DF__CUSTOMER_ACTIVE' and xtype = 'D')
ALTER TABLE [dbo].[customer] ADD CONSTRAINT [DF__CUSTOMER_ACTIVE] DEFAULT (1) FOR [active]

--END DEFAULTS FOR TABLE [customer]
GO

--BEGIN DEFAULTS FOR TABLE [film]
--ADD CONSTRAINT FOR '[film].[rating]'
if not exists(select * from sysobjects where name = 'DF__FILM_RATING' and xtype = 'D')
ALTER TABLE [dbo].[film] ADD CONSTRAINT [DF__FILM_RATING] DEFAULT ('G') FOR [rating]

--ADD CONSTRAINT FOR '[film].[rental_duration]'
if not exists(select * from sysobjects where name = 'DF__FILM_RENTAL_DURATION' and xtype = 'D')
ALTER TABLE [dbo].[film] ADD CONSTRAINT [DF__FILM_RENTAL_DURATION] DEFAULT (3) FOR [rental_duration]

--ADD CONSTRAINT FOR '[film].[rental_rate]'
if not exists(select * from sysobjects where name = 'DF__FILM_RENTAL_RATE' and xtype = 'D')
ALTER TABLE [dbo].[film] ADD CONSTRAINT [DF__FILM_RENTAL_RATE] DEFAULT (4.99) FOR [rental_rate]

--ADD CONSTRAINT FOR '[film].[replacement_cost]'
if not exists(select * from sysobjects where name = 'DF__FILM_REPLACEMENT_COST' and xtype = 'D')
ALTER TABLE [dbo].[film] ADD CONSTRAINT [DF__FILM_REPLACEMENT_COST] DEFAULT (19.99) FOR [replacement_cost]

--END DEFAULTS FOR TABLE [film]
GO

--BEGIN DEFAULTS FOR TABLE [staff]
--ADD CONSTRAINT FOR '[staff].[active]'
if not exists(select * from sysobjects where name = 'DF__STAFF_ACTIVE' and xtype = 'D')
ALTER TABLE [dbo].[staff] ADD CONSTRAINT [DF__STAFF_ACTIVE] DEFAULT (1) FOR [active]

--END DEFAULTS FOR TABLE [staff]
GO

--##SECTION END [CREATE DEFAULTS]

if not exists(select * from sys.objects where [name] = '__nhydrateschema' and [type] = 'U')
BEGIN
CREATE TABLE [__nhydrateschema] (
[dbVersion] [varchar] (50) NOT NULL,
[LastUpdate] [datetime] NOT NULL,
[ModelKey] [uniqueidentifier] NOT NULL,
[History] [nvarchar](max) NOT NULL
)
if not exists(select * from sys.objects where [name] = '__pk__nhydrateschema' and [type] = 'PK')
ALTER TABLE [__nhydrateschema] WITH NOCHECK ADD CONSTRAINT [__pk__nhydrateschema] PRIMARY KEY CLUSTERED ([ModelKey])
END
GO

if not exists(select * from sys.objects where name = '__nhydrateobjects' and [type] = 'U')
CREATE TABLE [dbo].[__nhydrateobjects]
(
	[rowid] [bigint] IDENTITY(1,1) NOT NULL,
	[id] [uniqueidentifier] NULL,
	[name] [varchar](500) NOT NULL,
	[type] [varchar](10) NOT NULL,
	[schema] [varchar](500) NULL,
	[CreatedDate] [datetime] NOT NULL,
	[ModifiedDate] [datetime] NOT NULL,
	[Hash] [varchar](32) NULL,
	[ModelKey] [uniqueidentifier] NOT NULL,
)

if not exists(select * from sys.indexes where name = '__ix__nhydrateobjects_name')
CREATE NONCLUSTERED INDEX [__ix__nhydrateobjects_name] ON [dbo].[__nhydrateobjects]
(
	[name] ASC
)

if not exists(select * from sys.indexes where name = '__ix__nhydrateobjects_schema')
CREATE NONCLUSTERED INDEX [__ix__nhydrateobjects_schema] ON [dbo].[__nhydrateobjects] 
(
	[schema] ASC
)

if not exists(select * from sys.indexes where name = '__ix__nhydrateobjects_type')
CREATE NONCLUSTERED INDEX [__ix__nhydrateobjects_type] ON [dbo].[__nhydrateobjects] 
(
	[type] ASC
)

if not exists(select * from sys.indexes where name = '__ix__nhydrateobjects_modelkey')
CREATE NONCLUSTERED INDEX [__ix__nhydrateobjects_modelkey] ON [dbo].[__nhydrateobjects] 
(
	[ModelKey] ASC
)

if not exists(select * from sys.indexes where name = '__pk__nhydrateobjects')
ALTER TABLE [dbo].[__nhydrateobjects] ADD CONSTRAINT [__pk__nhydrateobjects] PRIMARY KEY CLUSTERED 
(
	[rowid] ASC
)

